package com.lyhengtongwl.zqsnews.ui.activity;

import android.annotation.SuppressLint;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lyhengtongwl.zqsnews.App;
import com.lyhengtongwl.zqsnews.R;
import com.lyhengtongwl.zqsnews.adapter.NewsDDDetailAdapter;
import com.lyhengtongwl.zqsnews.base.BaseActivity;
import com.lyhengtongwl.zqsnews.entity.NewsBaseEntity;
import com.lyhengtongwl.zqsnews.entity.NewsDDDetailEntity;
import com.lyhengtongwl.zqsnews.net.MyCallback;
import com.lyhengtongwl.zqsnews.net.Task;
import com.lyhengtongwl.zqsnews.utils.NewsToastUtils;
import com.lyhengtongwl.zqsnews.utils.SPUtils;
import com.lyhengtongwl.zqsnews.utils.TitleBuilder;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import de.hdodenhof.circleimageview.CircleImageView;
import retrofit2.Response;

@SuppressLint("Registered")
public class NewsDDDetailActivity extends BaseActivity {
    @BindView(R.id.civ_head)
    CircleImageView civ_head;
    @BindView(R.id.tv_nickname)
    TextView tv_nickname;
    @BindView(R.id.tv_id)
    TextView tv_id;
    @BindView(R.id.recycle)
    RecyclerView recyclerView;
    private List<NewsDDDetailEntity> list = new ArrayList<>();
    private NewsDDDetailAdapter newsDDDetailAdapter;

    @Override
    public void setListen() {

    }

    @Override
    public void setTitleBarColor() {

    }

    @Override
    protected boolean isStatusBarBlack() {
        return false;
    }

    @Override
    public int intiLayout() {
        return R.layout.activity_dddetail;
    }

    @Override
    public void initView() {

    }

    @SuppressLint("SetTextI18n")
    @Override
    public void initData() {
        String imgUrl = getIntent().getStringExtra("imgUrl") + "";
        String id = getIntent().getStringExtra("id") + "";
        String nikeName = getIntent().getStringExtra("nikeName") + "";
        new TitleBuilder(this).setTitleText("徒弟详情")
                .setLeftIcoListening(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        finish();
                    }
                });

        Picasso.with(App.getContext()).load(imgUrl).placeholder(R.mipmap.head).error(R.mipmap.head).into(civ_head);
        tv_nickname.setText(nikeName);
        tv_id.setText("ID：" + id);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(manager);
        getList(SPUtils.get(App.getContext(), "userId", "") + "", id);
    }

    private void getList(String userId, String id) {
        Task.getApiService().getDiscipleDetails(userId, id).enqueue(new MyCallback<NewsBaseEntity>(NewsDDDetailActivity.this) {
            @Override
            protected void onSuccess(Response<NewsBaseEntity> response) {
                if ("1".equals(response.body().getCode())) {
                    String jsonStr = JSONObject.toJSONString(response.body().getData());
                    JSONArray jsonArray = JSONArray.parseArray(jsonStr);
                    list.clear();
                    for (int i = 0; i < jsonArray.size(); i++) {
                        NewsDDDetailEntity newsDDDetailEntity = new NewsDDDetailEntity();
                        JSONObject jsonObject = JSON.parseObject(jsonArray.get(i).toString());
                        newsDDDetailEntity.setMoney(jsonObject.getInteger("money")+"");
                        newsDDDetailEntity.setState(jsonObject.getInteger("status")+"");
                        newsDDDetailEntity.setTitle(jsonObject.getString("remark"));
                        list.add(newsDDDetailEntity);
                    }
                    newsDDDetailAdapter = new NewsDDDetailAdapter(list);
                    recyclerView.setAdapter(newsDDDetailAdapter);
                } else {
                    NewsToastUtils.showToast(App.getContext(), response.body().getMsg());
                }
            }

            @Override
            protected void onFailure(Throwable t) {
                NewsToastUtils.showToast(App.getContext(), "网络请求失败！");
            }
        });
    }
}
